package model;

import java.util.ArrayList;
import java.util.List;

public class ChatModel {
	private List<ChatMessage> data;
	public ChatModel() {
		data = new ArrayList<ChatMessage>();
		
	}
	public void addMessage(ChatMessage msg) {
		data.add(msg);
	}
	/** Get the index of the given message.
	 * We search backwards to find the input message because the parameter in the common use case is 
	 * more likely to be near the end of the list.
	 */
	public int getMessageIndex(ChatMessage msg) {
		if(msg==null) return -1;
		int index = data.size()-1;
		while(index>=0 && data.get(index)!=msg) {
			index--;
		}
		return index;
	}
	public ChatMessage getMessage(int index) {
		return data.get(index);
	}
	public int getNumMessages() {
		return data.size();
	}
}
